package com.manager.plat.dao.impl;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;

import org.apache.log4j.Logger;
import org.springframework.jdbc.core.simple.ParameterizedRowMapper;
import org.springframework.jdbc.core.support.JdbcDaoSupport;

import com.manager.plat.dao.ILimitUserNumDao;
import com.manager.plat.util.BaseUtil;
import com.manager.plat.vo.LimitUserNumVo;
/**
 * 用户当日拨打次数限制数据实现层
 * @author liugeng
 *
 */
public class LimitUserNumDaoImpl extends JdbcDaoSupport implements ILimitUserNumDao {
	private static Logger logger = Logger.getLogger(LimitUserNumDaoImpl.class);
	
	/**获得用户当日拨打次数限制列表*/
	@SuppressWarnings("unchecked")
	@Override
	public List<LimitUserNumVo> getLimitUserNumList() {
		String sql = "select id, day_num, status from tb_ivr_limit_user_num where status = 1";
		logger.info(String.format("获得用户当日拨打次数限制列表[%s]", sql));
		
		List<LimitUserNumVo> limitUserNumVoList = this.getJdbcTemplate().query(sql, rowMapper);
		if(limitUserNumVoList != null && limitUserNumVoList.size() > 0){
			return limitUserNumVoList;
		}
		return null;
	}
	/**根据Id获得用户用户当日拨打次数限制信息*/
	@SuppressWarnings("unchecked")
	@Override
	public LimitUserNumVo getLimitUserNumById(int id) {
		String sql = "select id, day_num, status from tb_ivr_limit_user_num where id = ? and status = 1";
		Object[] param = new Object[]{id};
		logger.info(String.format("根据Id获得用户用户当日拨打次数限制信息[%s]", BaseUtil.logSQL(sql, param)));
		
		List<LimitUserNumVo> limitUserNumVoList = this.getJdbcTemplate().query(sql, param, rowMapper);
		if(limitUserNumVoList != null && limitUserNumVoList.size() > 0){
			return limitUserNumVoList.get(0);
		}
		return null;
	}
	/**数据转换*/
	private ParameterizedRowMapper<LimitUserNumVo> rowMapper = new ParameterizedRowMapper<LimitUserNumVo>() {
		@Override
		public LimitUserNumVo mapRow(ResultSet rs, int rowNum) throws SQLException {
			LimitUserNumVo limitUserNumVo = new LimitUserNumVo();
			limitUserNumVo.setId(rs.getInt("id"));
			limitUserNumVo.setDay_num(rs.getInt("day_num"));
			limitUserNumVo.setStatus(rs.getInt("status"));
			return limitUserNumVo;
		}
	};
	/**新增用户用户当日拨打次数限制信息*/
	@Override
	public int insertLimitUserNum(LimitUserNumVo limitUserNumVo) {
		String sql = "insert into tb_ivr_limit_user_num(day_num, status, add_user) values(?,1,?)";
		Object[] param = new Object[]{limitUserNumVo.getDay_num(), limitUserNumVo.getAdd_user()};
		logger.info(String.format("新增用户用户当日拨打次数限制信息[%s]", BaseUtil.logSQL(sql, param)));
		
		int count = this.getJdbcTemplate().update(sql, param);
		return count;
	}
	/**更新用户用户当日拨打次数限制信息*/
	@Override
	public int updateLimitUserNum(LimitUserNumVo limitUserNumVo) {
		String sql = "update tb_ivr_limit_user_num set day_num = ?, add_user = ? where id = ?";
		Object[] param = new Object[]{limitUserNumVo.getDay_num(), limitUserNumVo.getAdd_user(),limitUserNumVo.getId()};
		logger.info(String.format("更新用户用户当日拨打次数限制信息[%s]", BaseUtil.logSQL(sql, param)));
		
		int count = this.getJdbcTemplate().update(sql, param);
		return count;
	}
	/**删除用户用户当日拨打次数限制信息*/
	@Override
	public int deleteLimitUserNum(int id) {
		String sql = "update tb_ivr_limit_user_num set status = 0 where id = ?";
		Object[] param = new Object[]{id};
		logger.info(String.format("删除用户用户当日拨打次数限制信息[%s]", BaseUtil.logSQL(sql, param)));

		int count = this.getJdbcTemplate().update(sql, param);
		return count;
	}
}
